package cn.lovecyy.mockjson.core.gen;

import cn.hutool.core.util.RandomUtil;
import java.util.HashMap;
import java.util.Map;

public class BankCardMockUtil {

	// 常见银行前缀
	private static final String[] BANK_PREFIXES = { "622202", "622848", "622700", "622262", "622155" };

	private static final String[] BANK_NAMES = { "中国工商银行", "中国建设银行", "中国农业银行", "中国银行", "招商银行", "交通银行" };

	private static final String[] CARD_TYPES = { "借记卡", "信用卡" };

	// 银行名称与联行号前缀映射
	private static final Map<String, String> CNAPS_PREFIX = new HashMap<>();

	// 银行名称与银行编码映射（编码格式如C10102）
	private static final Map<String, String> BANK_CODE_MAP = new HashMap<>();
	static {
		CNAPS_PREFIX.put("中国工商银行", "102");
		CNAPS_PREFIX.put("中国建设银行", "105");
		CNAPS_PREFIX.put("中国农业银行", "103");
		CNAPS_PREFIX.put("中国银行", "104");
		CNAPS_PREFIX.put("招商银行", "308");
		CNAPS_PREFIX.put("交通银行", "301");
		// 银行编码（用户指定格式）
		BANK_CODE_MAP.put("中国人民银行", "C00001");
		BANK_CODE_MAP.put("中国工商银行", "C10102");
		BANK_CODE_MAP.put("中国农业银行", "C10103");
		BANK_CODE_MAP.put("中国银行", "C10104");
		BANK_CODE_MAP.put("中国建设银行", "C10105");
		BANK_CODE_MAP.put("招商银行", "C30001"); // 示例编码
		BANK_CODE_MAP.put("交通银行", "C30002"); // 示例编码
	}

	/**
	 * 随机生成银行卡号（16~19位，Luhn校验）
	 */
	public static String randomBankCardNumber() {
		String prefix = BANK_PREFIXES[RandomUtil.randomInt(BANK_PREFIXES.length)];
		int length = 16 + RandomUtil.randomInt(0, 4); // 16~19位
		StringBuilder sb = new StringBuilder(prefix);
		for (int i = prefix.length(); i < length - 1; i++) {
			sb.append(RandomUtil.randomInt(0, 10));
		}
		sb.append(calcLuhnCheckDigit(sb.toString()));
		return sb.toString();
	}

	/**
	 * Luhn算法计算校验位
	 */
	public static char calcLuhnCheckDigit(String number) {
		int sum = 0;
		boolean alternate = true;
		for (int i = number.length() - 1; i >= 0; i--) {
			int n = number.charAt(i) - '0';
			if (alternate) {
				n *= 2;
				if (n > 9)
					n -= 9;
			}
			sum += n;
			alternate = !alternate;
		}
		int check = (10 - (sum % 10)) % 10;
		return (char) ('0' + check);
	}

	/**
	 * 随机银行名称
	 */
	public static String randomBankName() {
		return BANK_NAMES[RandomUtil.randomInt(BANK_NAMES.length)];
	}

	/**
	 * 随机卡类型
	 */
	public static String randomCardType() {
		return CARD_TYPES[RandomUtil.randomInt(CARD_TYPES.length)];
	}

	/**
	 * 随机银行预留手机号
	 */
	public static String randomBankPhone() {
		return "1" + RandomUtil.randomInt(3, 10) + RandomUtil.randomNumbers(9);
	}

	/**
	 * 随机联行号（12位）
	 */
	public static String randomCnapsCode(String bankName) {
		String prefix = CNAPS_PREFIX.getOrDefault(bankName, "999");
		StringBuilder sb = new StringBuilder(prefix);
		while (sb.length() < 12) {
			sb.append(RandomUtil.randomInt(0, 10));
		}
		return sb.toString();
	}

	/**
	 * 生成完整银行卡信息
	 */
	public static Map<String, Object> randomBankCardInfo() {
		String bankName = randomBankName();
		Map<String, Object> map = new HashMap<>();
		map.put("bankCardNo", randomBankCardNumber());
		map.put("bankName", bankName);
		map.put("cardType", randomCardType());
		map.put("bankPhone", randomBankPhone());
		map.put("cnapsCode", randomCnapsCode(bankName));
		map.put("bankCode", BANK_CODE_MAP.getOrDefault(bankName, "C99999"));
		return map;
	}

}
